// pages/addstudent/index.js
import urls from '../../utils/urls.js';
import loginFn from '../../utils/login.js';
var app = getApp();

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		sexIdx: 0, //默认性别
		sex: ['未知', '男', '女'], //性别
		date: '2014-01-01', //默认出生日期
		grade: [], //年级
		gradeIdx: 0, //默认年级
		school: '', //学校
		logoimg: '/img/addstudent.png', //头像
		studentImgId: 0, //头像id
		subBtn: false
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log('options:',options);
		var that = this;
		var loginInfo = loginFn()
		wx.request({
			url: urls.studentGrade,
			data: loginInfo,
			success: function(res) {
				console.log(res)
				if (res.data.code == '0') {
					that.setData({
						grade: res.data.data.grade
					})
				};
			}
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	bindsexChange: function(e) { //选择性别
		this.setData({
			sexIdx: e.detail.value
		})
	},
	binddateChange: function(e) { //选择日期
		console.log(e)
		this.setData({
			date: e.detail.value
		})
	},
	bindgradeChange: function(e) { //选择年级
		this.setData({
			gradeIdx: e.detail.value
		})
	},
	saveimgFn: function(e) { //保存头像
		var that = this;
		wx.chooseImage({
			count: 1,
			sizeType: ['compressed'],
			sourceType: ['album', 'camera'],
			success(res) {
				var tempFilePaths = res.tempFilePaths;
				app.uploadImage(tempFilePaths[0], (res) => {
					if(res.code == 1) {
						that.setData({
							logoimg: tempFilePaths,
							studentImgId: res.data.imageFileId
						})
					} else {
						app.alert(res.msg);
					}
				})
			}
		})
	},
	saveFn: function(e) { //保存信息
		console.log(e)
		this.setData({
			subBtn: true
		})
		var that = this;
		var datas = e.detail.value;
		var cansave = true;
		for (var key in datas) {
			if (key == 'studentName' || key == 'school') {
				if (datas[key] == "") {
					wx.showToast({
						title: '请完善资料',
						icon: 'none'
					})
					cansave = false
					that.setData({
						subBtn: false
					})
					break
				};
			};
			if (key == 'gradeId') {
				datas[key] = this.data.grade[datas[key]].gradeId
			};
		}

		datas['studentImg'] = this.data.studentImgId

		if (cansave) {
			var loginInfo = loginFn()
			for (var key in loginInfo) {
				datas[key] = loginInfo[key]
			}
			wx.request({
				url: urls.studentSave,
				data: datas,
				method: 'POST',
				success: function(res) {
					console.log(res)
					if (res.data.code == '0') {
						wx.showToast({
							title: '保存成功',
						})
						setTimeout(function() {
							var year = 1000 * 60 * 60 * 24 * 365;
							var now = new Date();
							var birth = that.data.date;
							birth = Date.parse(birth.replace('/-/g', "/"));
							var birthday = new Date(birth);
							var age = parseInt((now - birthday) / year);
							var questionTypeId = 1
							if (age > 6) {
								questionTypeId = 2
							};
							wx.navigateTo({
								url: '/pages/test/index?studentId=' + res.data.data.studentId + '&questionTypeId=' + questionTypeId,
							})
						}, 2000)
					};
				}
			})
		};
	},
	resetFn: function(e) { //取消

	}
})
